package data;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class JDBCAficionDAO implements IAficionDAO {
	
	
	public JDBCAficionDAO(){
	}
	
	
	

	public List<String> selectAficiones(Connection conn,int idUsuario) {
		List<String> resultado = new ArrayList<String>();
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        String aficion = null;
	        String sql = "SELECT ID_AFICION FROM aficiones_us WHERE (ID_USUARIO=?);";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setInt(1, idUsuario);
	            result = stmt.executeQuery();

	            while (result.next()) {
	            	int idAficion = result.getInt("ID_AFICION");
	            	 aficion = selectAficion(idAficion);
	            	 resultado.add(aficion);
	            }
	            
	           
	        } catch (SQLException e) {
	            System.out.println("selectAficiones Message: " + e.getMessage());
	            System.out.println("selectAficiones SQLState: " + e.getSQLState());
	            System.out.println("selectAficiones ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

	        return resultado;
	}

	
	public String selectAficion(int idAficion) {
		Connection conn= ConnectionManager.getInstance().CheckOut();
				PreparedStatement stmt = null;
		        ResultSet result = null;
		        String aficion = null;
		        String sql = "SELECT * FROM aficion WHERE (ID_AFICION=?);";

		        try {
		            stmt = conn.prepareStatement(sql);
		            stmt.setInt(1, idAficion);
		            result = stmt.executeQuery();

		            if (result.next()) {
		            	 aficion =result.getString("NOMBRE");
		            }
		            
		           
		        } catch (SQLException e) {
		            System.out.println("selectAficion Message: " + e.getMessage());
		            System.out.println("selectAficion SQLState: " + e.getSQLState());
		            System.out.println("selectAficion ErrorCode: " + e.getErrorCode());
		        } finally {
		        	ConnectionManager.getInstance().CheckIn(conn);
		            try {
		                if (result != null) {
		                    result.close();
		                }
		                if (stmt != null) {
		                    stmt.close();
		                }
		            } catch (SQLException e) {
		            }
		        }

		        return aficion;
		}
	
	
	public static void main(String[] arg){
//		
//		Connection conn= ConnectionManager.getInstance().CheckOut();
//		JDBCAficionDAO dao= new JDBCAficionDAO();
//		System.out.println(dao.selectAficion(conn, 8));
//		
//		ConnectionManager.getInstance().CheckIn(conn);
	}
	
}
